<template>
	<view>
		<uni-nav-bar left-icon="back" left-text="返回" fixed="true" statusBar="true" @clickLeft="leftAction" title="基站管理" ></uni-nav-bar>
		<view v-if="dataList.length==0" class="uni-goonbtn-v">
			<button type="default" @click="debugAction">获取基站信息</button>
		</view>
		<form @submit="formSubmit">
			<view v-for="(model,i) in dataList">
				<uni-section :title="model.title" type="line"></uni-section>
				<template v-for="(item,index) in model.list">
					<uni-list-item :note="item.title" :rightText="item.value">
						<template slot="footer">
							<input class="uni-input" :disabled="!item.enable" :name="item.enable?item.key:''"
							v-model="item.value" />
								<!-- :value="item.value" --> 
								
						</template>
					</uni-list-item>
				</template>
			</view>
			<view v-if="dataList.length>0" class="uni-goonbtn-v">
				<button type="default" @click="formSubmit">修改</button>
			</view>
		</form>

	</view>
</template>

<script>
	import uniNavBar from '@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue'
	import wsconnect from '@/api/webservice.js'
	import UniSection from '@/uni_modules/uni-section/components/uni-section/uni-section.vue'

	export default {
		data() {
			return {
				dataList: []
			};
		},
		onLoad(options) {
			this.title = options.title
			this.bs_id = options.id
			this.$http.get('/netty/station/' + this.bs_id).then(res => {
				const form = res.data.data
				this.form = form
			}).catch(err => {
				uni.showModal({
					content: '出错了，请稍后再试',
					showCancel: false
				})
			})
			uni.hideLoading()

		},
		components: {
			uniNavBar
		},
		methods: {
			leftAction() {
				uni.navigateBack({
					delta: 1
				});
			},
			formSubmit: function(e) {//修改基站
				var time = this.$moment().format("yyyyMMDDhhmmss");
				
				var str = 'BsConfigRq,/T='+time +'/'
				for (let item of this.dataList) {
					for (let s of item.list) {
						if (s.enable) {
							console.log(s.value)
							str += (s.key + '=' + s.value + '/')
						}
					}
				}
				console.log("请求参数是："+str)
				const sendCmdTagName = this.form.sendCmdTagName
				const reqTagName = this.form.reqTagName

				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: [str],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					const arr = res.split('/')
					this.configDataList(arr)
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			debugAction() {
				const sendCmdTagName = this.form.sendCmdTagName
				const reqTagName = this.form.reqTagName

				wsconnect.requestJson({
					items: [sendCmdTagName + '.DESC'],
					vals: ['BsInqRq,//'],
					type: "SetDataByName",
				}, reqTagName).then(res => {
					const arr = res.split('/')
					this.configDataList(arr)
					wsconnect.clearReqData(1, reqTagName)
				}).catch(error => {
					if (error.code == 1) {
						uni.redirectTo({
							url: "../index/index"
						})
					} else {
						uni.showModal({
							content: error.msg,
							showCancel: false
						})
					}
				})
			},
			configDataList(arr){//配置当前显示数据
				const array = arr.slice(1, arr.length - 1)
				var dataDic = {}
				for (let i in array) {
					const result = array[i]
					const a = result.split('=')
					dataDic[a[0]] = a[1]
				}
				var data_list = []
				var item_list = []
				item_list.push({
					title: "基站",
					value: this.form.baseStationId,
					enable: false,
					key: ''
				})
				item_list.push({
					title: "基站时间",
					value: dataDic["T"],
					enable: false,
					key: 'T'
				})
				item_list.push({
					title: "硬件版本",
					value: dataDic["HW"],
					enable: false,
					key: 'HW'
				})
				item_list.push({
					title: "软件版本",
					value: dataDic["FW"],
					enable: false,
					key: 'FW'
				})
				item_list.push({
					title: "SIM卡",
					value: dataDic["4GSIM"],
					enable: false,
					key: '4GSIM'
				})
				item_list.push({
					title: "信号质量",
					value: dataDic["4GCSQ"],
					enable: false,
					key: '4GCSQ'
				})
				item_list.push({
					title: "配置开关",
					value: dataDic["CFG"],
					enable: false,
					key: 'CFG'
				})
				data_list.push({
					title: "基本信息",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "配置通道",
					value: dataDic["CG"],
					enable: true,
					key: 'CG'
				
				})
				item_list.push({
					title: "数据通道1",
					value: dataDic["D1"],
					enable: true,
					key: 'D1'
				})
				item_list.push({
					title: "数据通道2",
					value: dataDic["D2"],
					enable: true,
					key: 'D2'
				})
				item_list.push({
					title: "报警通道",
					value: dataDic["AM"],
					enable: true,
					key: 'AM'
				})
				data_list.push({
					title: "信道号",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "IP地址",
					value: dataDic["LI"],
					enable: true,
					key: 'LI'
				})
				item_list.push({
					title: "子网掩码",
					value: dataDic["LSUB"],
					enable: true,
					key: 'LSUB'
				})
				item_list.push({
					title: "网关",
					value: dataDic["LGAT"],
					enable: true,
					key: 'LGAT'
				})
				item_list.push({
					title: "端口",
					value: dataDic["LPT"].replace('\r\n',''),
					enable: true,
					key: 'LPT'
				})
				data_list.push({
					title: "基站",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "IP地址",
					value: dataDic["SI"],
					enable: true,
					key: 'SI'
				})
				item_list.push({
					title: "子网掩码",
					value: dataDic["SSUB"],
					enable: true,
					key: 'SSUB'
				})
				item_list.push({
					title: "网关",
					value: dataDic["SGAT"],
					enable: true,
					key: 'SGAT'
				})
				item_list.push({
					title: "端口",
					value: dataDic["SPT"].replace('\r\n',''),
					enable: true,
					key: 'SPT'
				})
				data_list.push({
					title: "服务器",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "名称",
					value: dataDic["WSID"],
					enable: true,
					key: 'WSID'
				})
				item_list.push({
					title: "密码",
					value: dataDic["WPW"],
					enable: true,
					key: 'WPW'
				})
				data_list.push({
					title: "WIFI",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "数据",
					value: dataDic["DS"],
					enable: false,
					key: 'DS'
				})
				item_list.push({
					title: "报警",
					value: dataDic["AS"],
					enable: false,
					key: 'AS'
				})
				item_list.push({
					title: "WIFI登录次数",
					value: dataDic["WS"],
					enable: false,
					key: 'WS'
				})
				data_list.push({
					title: "离线数据",
					list: item_list
				})
				item_list = []
				item_list.push({
					title: "总数",
					value: dataDic["DT"],
					enable: false,
					key: 'DT'
				})
				item_list.push({
					title: "数据通道1",
					value: dataDic["D1T"],
					enable: false,
					key: 'D1T'
				})
				item_list.push({
					title: "数据通道2",
					value: dataDic["D2T"],
					enable: false,
					key: 'D2T'
				})
				data_list.push({
					title: "仪表数量",
					list: item_list
				})
				this.dataList = data_list
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/common/uni-nvue.scss';

	.uni-goonbtn-v {
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-top: 40rpx;
		height: 40rpx;
	}

	.uni-input {
		background-color: #F1F1F1;
		height: 60rpx;
		width: 520rpx;
	}

	.uni-input :disabled {
		background-color: #FFFFFF;
		border: 0px;
	}

	.uni-input :enabled {
		border: 1px solid #DDD;
	}
</style>
